<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4166 2010-07-31 16:36:26Z huuphuoc $
 * @since		2.0.6
 */
?>
<?php 
$this->headTitle($this->translator('mail_list_page_title'));
$this->headScript()->appendFile($this->APP_STATIC_SERVER . '/public/js/jquery.json-2.3.min.js');
?>

<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/mail-48x48.png'; ?>" alt=""/>
    <span><?php echo $this->translate('mail_list_breadcrumb'); ?></span>
</div>
<?php
//nếu có message thì hiển thị
if(!empty($this->message)){
	echo '<div id="message">'.$this->message.'</div>';
}
?>
<form id="frmActions" action="<?php echo $this->url(array(), 'mail_mail_list'); ?>" method="post">
    <div id="actions">
            <?php if($this->allow('send')){?>
            <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('mail_compose') ?>" onclick="location = '<?php echo $this->url(array(), 'mail_mail_send');?>';"/>
            <?php }?>
            <?php if($this->allow('delete')){?>
            <input type="button" id="btnDelete" class="button" value="<?php echo $this->translate('mail_delete_many')?>"/>
            <?php } ?>
    </div>
    <div id="filters">
        <div class="f-data" >
            <label><?php echo $this->translate('mail_list_filter_type'); ?>:</label>
            <select name="type" id="type">
                <option value="">---</option>
                <option value="inbox"><?php echo $this->translate('mail_list_filter_type_inbox')?></option>
                <option value="sent"><?php echo $this->translate('mail_list_filter_type_sent')?></option>
            </select>
        </div>
        <div class="f-data" >
            <label><?php echo $this->translate('mail_list_filter_status'); ?>:</label>
            <select name="is_read" id="is-read">
                <option value="">---</option>
                <option value="1"><?php echo $this->translate('mail_list_filter_status_is_read')?></option>
                <option value="0"><?php echo $this->translate('mail_list_filter_status_is_not_read')?></option>
            </select>
        </div>
        
        <div class="f-data">
            <label><?php echo $this->translator('mail_list_filter_created_user_id'); ?>:</label>
            <?php echo $this->helperLoader('core')->userSelect(array(
                                                        'id'       => 'created_user_id',
                                                        'name'     => 'created_user_id',
                                                        'selected' => (isset($this->exp['created_user_id']) ? $this->exp['created_user_id'] : null)
                                                ));
            ?>
        </div>

        <div class="f-submit">
            <input type="submit" class="button" value="<?php echo $this->translate('mail_list_search_button'); ?>" />
        </div>
    </div>
    <table class="list" cellspacing="0">
        <thead>
            <tr>
                <td width="3%">
                        <input type="checkbox" class="chkAll"/>
                </td>
                <td width="35%">
                        <?php echo $this->translator('mail_subject'); ?>
                </td>
                <td width="15%">
                        <?php echo $this->translator('mail_from_mail'); ?>
                </td>
                <td width="15%">
                        <?php echo $this->translate('mail_send_to'); ?>
                </td>
                <td width="10%">
                        <?php echo $this->translate('mail_sent_date'); ?>
                </td>
                <td width="15%">
                        <?php echo $this->translate('mail_list_actions'); ?>
                </td>
            </tr>
        </thead>
        <tbody>
            <?php if ($this->numMails == 0) : ?>
            <tr><td colspan ="6" style="color: #fc0202;"><?php echo $this->translator('mail_list_not_found'); ?></td></tr>
            <?php else : 
                $i=0;
            ?>
            <?php foreach ($this->mails as $mail) : ?>
            <?php
                $class= 'class="'. (($i == $this->numMails - 1) ? 'last' : '') . ((!$mail->is_read) ? ' unread' : '') . '"';
            ?>
            <tr id="mail-<?php echo $mail->id?>" <?php echo $class?>>
                <td>
                    <input type="checkbox" class="chkId" value="<?php echo $mail->id;?>"/>
                </td>
                <td><?php echo $mail->subject?></td>
                <td><?php echo $mail->from_name?> (<?php echo $mail->from_mail?>)</td>
                <td><?php echo $mail->to_name?> (<?php echo $mail->to_mail?>)</td>
                <td><?php echo date('m-d-Y',strtotime($mail->sent_date))?></td>
                <td>
                    <?php if($this->allow('details')) :?>
                    <a href="<?php echo $this->url(array('id' => $mail->id),'mail_mail_details')?>" ><?php echo $this->translate('mail_list_actions_details')?></a>
                    |
                    <?php endif;?>
                    <?php if($this->allow('delete')) :?>
                    <a href="" class="deleteAction" rel="<?php echo $mail->id?>"><?php echo $this->translate('mail_list_actions_delete')?></a>
                    <?php endif;?>
                </td>
            </tr>
            <?php $i++;?>
            <?php endforeach;?>
            <?php endif;?>
        </tbody>
        <tfoot>
            <tr>
                <td>
                        <input type="checkbox" class="chkAll"/>
                </td>
                <td>
                        <?php echo $this->translator('mail_subject'); ?>
                </td>
                <td>
                        <?php echo $this->translator('mail_from_mail'); ?>
                </td>
                <td>
                        <?php echo $this->translate('mail_send_to'); ?>
                </td>
                <td>
                        <?php echo $this->translate('mail_sent_date'); ?>
                </td>
                <td>
                        <?php echo $this->translate('mail_list_actions'); ?>
                </td>
            </tr>
        </tfoot>
    </table>
</form>

<?php if ($this->numMails > 0) :?>
<div id="pagination">
        <?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
</div>
<?php endif; ?>
<script type="text/javascript">
    $(document).ready(function(){
    <?php if($this->allow('delete')){?>
            $('.deleteAction').click(function(){
                if(confirm('<?php echo $this->translate('mail_list_delete_confirm')?>')){
                    var id = $(this).attr('rel');
                    $.ajax({
                        url: '<?php echo $this->url(array(), 'mail_mail_delete')?>',
                        data: {id: id},
                        type: 'POST',
                        success: function(response){
                            var data = $.evalJSON(response);
                            if(data.status){
                                $('#mail-' + id).fadeOut();
                            }else{
                                alert(data.message);
                            }
                        }
                    });
                }
                return false;
            });
            $("#btnDelete").click(function(){
		var arrChkId = $(".chkId");
		var ids = new Array();
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
                      ids.push(arrChkId.eq(i).val());
			}
		}
            if(ids.length > 0){
			if(confirm("<?php echo $this->translate('mail_list_delete_many_confirm') ?>")){
				$.ajax({
                            type: 'POST',
                            url: "<?php echo $this->url(array(), 'mail_mail_delete'); ?>",
                            data: { id: $.toJSON(ids) },
                            success: function(response) {
                                var data = $.evalJSON(response);
                                if (data.status) {
                                    for(var i in ids){
                                        $('#mail-' + ids[i]).fadeOut();
                                    }
                                }else{
                                    alert(data.message);
                                }
                            }
                        });
			}else{
				return false;
			}
		}else{
			alert("<?php echo $this->translate('mail_list_bulk_action_none_selected')?>");
			return false;
		}
	});
    <?php } ?>
    });
    
</script>